package org.roger.search;

import java.io.IOException;

import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TermRangeQuery;

/**
 * 范围搜索
 * @param field 带查询的域
 * @param start 域的开始
 * @param end	域的结束
 */
public class RangeSearch<T> extends LuceneSearchBase{
	
	private T start;
	private T end;
	private String field;
	private boolean includeLower;
	private boolean includeUpper;
	
	public RangeSearch(String field,T start,T end, boolean includeLower, boolean includeUpper){
		this.field = field;
		this.start = start;
		this.end = end;
		this.includeLower = includeLower;
		this.includeUpper = includeUpper;
	}

	@Override
	public Query getSearchQuery() {
		
		Query query = null;
		if(start instanceof String){
			query =  TermRangeQuery.newStringRange(field,(String)start,(String)end,includeLower,includeUpper);
		}else if(start instanceof Integer){
			query =  NumericRangeQuery.newIntRange(field,(Integer)start,(Integer)end,includeLower,includeUpper);
		}else if(start instanceof Long){
			query =  NumericRangeQuery.newLongRange(field,(Long)start,(Long)end,includeLower,includeUpper);
		}else if(start instanceof Float){
			query =  NumericRangeQuery.newFloatRange(field, (Float)start, (Float)end, includeLower, includeUpper);
		}else if(start instanceof Double){
			query =  NumericRangeQuery.newDoubleRange(field, (Double)start, (Double)end, includeLower, includeUpper);
		}else{
			System.out.println("The params type is error!!!");
		}
		System.out.println(query.toString());
		return query;
	}
	
	public static void main(String[] args) {
		
		RangeSearch<Integer> search = new RangeSearch<Integer>("id",1,3,false,true);
		try {
			search.search("D:/index2");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
//		RangeSearch<Long> search = new RangeSearch<Long>("modified",1417677720630l,1417678121062l,true,true);
//		try {
//			search.search("D:/index");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Sort getSort() {
		// TODO Auto-generated method stub
		return null;
	}

}
